Xceed DataGrid for Silverlight Documentation
Selecting Data

Items in a grid can be selected both through end-user interactions and programmatically. The SelectionMode property defines how the items in a grid can be selected through end-user interactions. SelectionMode.Single indicates that only one item can be selected at a time. Multiple indicates that multiple items can be selected without pressing the CTRL or SHIFT modifier keys. Extended (default) indicates that multiple items can be selected by pressing the modifier keys. 

CTRL-PageUp/PageDown will unselect any active selection and move to the first or last item, respectively. CTRL-HOME/END will also unselect any active selection; however, it will move to and select the first or last item, respectively.

Retrieving the Selected Items

The selected data items can be retrieved through the SelectedItems property, which return all the data items that are currently selected, and the SelectedItem property, which returns the last data item that was selected.

The SelectedItem and SelectedItems properties can only be used with a synchronous data source. When used with a asynchronous data source, an exception will be thrown.
<sldg:DataGridControl x:Name="sldgDataGridControl"
                      ItemsSource="{Binding Path=People}">
   <sldg:DataGridControl.FixedFooters>
      <StackPanel Orientation="Horizontal">
         <TextBlock Text="{Binding ElementName=sldgDataGridControl,
                                   Path=SelectedItem.FirstName, StringFormat='{}{0} '}" />
         <TextBlock Text="{Binding ElementName=sldgDataGridControl,
                                   Path=SelectedItem.LastName, StringFormat='{}{0} '}" />
         <TextBlock Text="{Binding ElementName=sldgDataGridControl,
                                   Path=SelectedItems.Count, StringFormat='({0} items are selected)'}" />
      </StackPanel>
   </sldg:DataGridControl.FixedFooters>
</sldg:DataGridControl>

By using the BeginGetSelectedItems and EndGetSelectedItems asynchronous methods, the items that are currently selected in a grid can be retrieved. When dealing with a local data source (i.e., "full list"), the BeginGetSelectedItems method will return immediately and the value of the IsCompleted property of the returned IAsyncResult will be true indicating that the operation has been completed and the EndGetSelectedItems method can be called to finalize the process and get the selected items. If a virtualized data source is used, the EndGetSelectedItems method needs to be called in the callback after verifying whether the operation completed synchronously or not.

Retrieving the selected items asynchronously

Selecting Items Programmatically

Items can be selected by adding selection ranges to a grid's SelectedRanges property. Each range that is added to the collection represents a group of items that have matched one or more selection criteria. Programmatically, a selection range can only be created if the data it is working with is sorted. In order to sort the data, one or more SortDescription objects, which will be used to create the data query that is sent to the data source, must be provided at construction.

A selection range defines "start" and "end" range-information dictionaries that specify the start and end points of a range of selected items (see StartRangeInfos and EndRangeInfos properties). For each start/end range-information dictionary combination that is added to the selection range, a corresponding sort description, whose PropertyName property value matches the key of the start/end range-information dictionary combination, must also be provided (see examples below).

The sort descriptions used by a selection range have no impact on how the data is actually sorted in the grid and vice-versa.

In addition, both the start/end range-information dictionaries (i.e., StartRangeInfos and EndRangeInfos, respectively) expose an IsInclusive property that determines whether the start or end range-information dictionaries are inclusive, indicating that their values are included in the selection range (default), or if they are excluded from the selection range. These properties apply to all the range-information dictionary combinations specified in the StartRangeInfos and EndRangeInfos dictionaries. 

Items from a local, synchronous data source can also be added to a selection range through the FromItem and ToItem methods, which are defined by the StartRangeInfos and EndRangeInfos dictionaries, and represent the item from which to start the selection range and the item at which the range ends, respectively (see Selecting from and to a specific item example below). When specifying a start and end item as the selection range, the items will be selected according to their position in the data source and not how they are displayed on screen. This means that all items between the start and end items will be selected, regardless of where the start and end items are located visually.  

Predicates (i.e., filter parameter in the SelectionRange ctors) are executed locally meaning that when the BeginGetSelectedItems and EndGetSelectedItems are called and a selection range that provides a predicate is used, items will be retrieved from the server in order to determine those that pass the filter.

If one or more start and end range information is provided and/or a filter expression, only the items that are not already excluded by the range information and filter expression will be retrieved.

In a virtualized environment, it is not recommended to use the filter predicate unless dealing with a very limited number of items.

Items can also be unselected by creating a selection range and setting its SelectionType property to Unselect (see Unselecting items from a selection range example below). 

Now that you are thoroughly confused, take a look at the examples below, which cover most, if not all, possible scenarios. 

Selecting all items

Selecting specific items (ShipCountry = "Germany")

Selecting items within a range (ShipCountry = "A-M")

Selecting items using a filter expression (ShipVia !=3)

Selecting items using combined filter expressions (ShipVia = 3 OR OrderDate >= 2008/01/01)

Selecting items using multiple selection-range infos (ShipVia = 2 AND OrderDate = 2006)

Selecting items using a predicate delegate (ShippedDate > RequiredDate)

Selecting items using multiple selection ranges (ShipCountry = "A-D" AND ShipCountry = "M-S")

Selecting items from start to specified end point

Selecting items from specified start point to end

"Unselecting" items from a selection range

Selecting from and to a specific item (LOCAL LIST ONLY)

Selecting a single item from a remote data source (WCF DataServiceQuery)

Selection-Changed Notifications

Selection-changed notifications are provided via the SelectionChanged event, which is raised whenever the selection in a grid changes whether by end-user interaction or through programmatic modifications of the selected ranges.

Send Feedback